package com.workflow.workflowproject.config;

import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
import org.apache.logging.log4j.LogManager;
import org.openjdk.nashorn.internal.runtime.logging.Logger;
import org.slf4j.*;
import org.mybatis.logging.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import java.text.SimpleDateFormat;
import java.util.Date;

@Logger
@Configuration
public class MyBatisFlexConfiguration {

    private static final org.apache.logging.log4j.Logger log = LogManager.getLogger(MyBatisFlexConfiguration.class);

    public MyBatisFlexConfiguration() {
        log.info(">>>>>>>>>>>>>>>>MybatisFlex配置开始>>>>>>>>>>>>>>>");
        FlexGlobalConfig globalConfig = FlexGlobalConfig.getDefaultConfig();
//        //设置数据库正常时的值
        globalConfig.setNormalValueOfLogicDelete(1);
//        //设置数据已被删除时的值
        globalConfig.setDeletedValueOfLogicDelete(0);
        LogicDeleteManager.setProcessor(new LogicDeleteProcessorConfig());
        FlexGlobalConfig.getDefaultConfig().setTenantColumn("tenant_id");//全局配置租户字段
        //开启审计功能
        AuditManager.setAuditEnable(true);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //设置 SQL 审计收集器
        AuditManager.setMessageCollector(auditMessage ->{
            log.info("{},耗时：{}ms, 执行时间点：{},数据库IP:{}", auditMessage.getFullSql(), auditMessage.getElapsedTime(),sdf.format(new Date(auditMessage.getQueryTime())),auditMessage.getHostIp());
        });
        log.info(">>>>>>>>>>>>>>>>MybatisFlex配置结束>>>>>>>>>>>>>>>");
    }
}
